﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using MyGeneration.dOOdads;
using MethodIT.Helper;

using Hr20.DAL;
using Hr20.BLL;
using Hr20.ProcessFlow;


namespace Appraisal.Web.ZB
{
    public partial class CopyKpTable : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int ID = HelperURL.GetIntFromUrlString("ID");
                KP_Master kp = new KP_Master();
                if (!kp.LoadByPrimaryKey(ID))
                {
                    HelperMessage.MessageUrl("评审表无效！", "../Appraisal/AppraisalList.aspx");
                    return;
                }
                else
                {
                    txtName.Text = "新建 " + kp.Title;
                }
            }
        }

        private void LoadSubject()
        {
            KP_Subject s = new KP_Subject();
            s.Where.BeginDate.Value = DateTime.Now;
            s.Where.BeginDate.Operator = WhereParameter.Operand.LessThanOrEqual;
            s.Where.EndDate.Value = DateTime.Now;
            s.Where.EndDate.Operator = WhereParameter.Operand.GreaterThanOrEqual;

            //增加项目经理只能看自已建的项目
            string sUserID = DBController.GetInstance().GetAuthenticatedUserID();
            if (!DBController.GetInstance().CheckUserRole(sUserID, "DeptAdmin"))
            {
                s.Where.Creater.Value = sUserID;
            }

            s.Query.Load();

            FunctionSet.FillListControl(dplSubject, s.DefaultView.Table, KP_Subject.ColumnNames.SubjectName, KP_Subject.ColumnNames.SubjectName);
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            int KpID = HelperURL.GetIntFromUrlString("ID");
            int newID = CopyKp(KpID);
            if (newID>0)
            {
                KP_Master kp = new KP_Master();
                kp.LoadByPrimaryKey(newID);
                kp.Title = dplSubject.SelectedValue + txtName.Text;
                kp.Save();
                HelperMessage.MessageUrl("复制完成！", "../Appraisal/AppraisalList.aspx");
            }
        }

        public int CopyKp(int KpID)
        {
            int nNewID = 0;
            KP_Master new_kp = new KP_Master();
            KP_Relation new_relation = new KP_Relation();
            KP_Template new_item = new KP_Template();
            //KP_Template_Desc new_itemdesc = new KP_Template_Desc();

            CopyKp(KpID, new_kp);
            CopyRelation(KpID, new_kp.KP_ID, new_relation);
            CopyItem(KpID, new_kp.KP_ID, new_item);

            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();
            try
            {
                tx.BeginTransaction();
                new_kp.Save();
                new_relation.Save();
                new_item.Save();
                tx.CommitTransaction();
                nNewID = new_kp.KP_ID;
            }
            catch
            {
                tx.RollbackTransaction();
                TransactionMgr.ThreadTransactionMgrReset();
            }

            return nNewID;
        }

        private void CopyItem(int oldID, int newID, KP_Template new_item)
        {
            KP_Template kp = new KP_Template();
            kp.Where.KP_ID.Value = oldID;
            kp.Where.Is_Group.Value = true;
            kp.Query.Load();
            while (!kp.EOF)
            {
                new_item.AddNew();
                int newItemID = SequenceGenerator.GetNextVal("KP_Item_ID");
                new_item.Item_ID = newItemID;
                new_item.Item_Name = kp.Item_Name;
                new_item.Min_Score = kp.Min_Score;
                new_item.Max_Score = kp.Max_Score;
                new_item.KP_ID = newID;
                new_item.Item_Quotiety = kp.Item_Quotiety;
                new_item.Is_Group = kp.Is_Group;
                new_item.Parent_Item_ID = kp.Parent_Item_ID;
                new_item.Item_Type = kp.Item_Type;
                new_item.Item_Width = kp.Item_Width;
                new_item.Item_Default = kp.Item_Default;
                new_item.Item_Desc = kp.Item_Desc;

                KP_Template kpitem = new KP_Template();
                kpitem.Where.Parent_Item_ID.Value = kp.s_Item_ID;
                kpitem.Where.Is_Group.Value = false;
                kpitem.Query.Load();
                while (!kpitem.EOF)
                {
                    new_item.AddNew();
//                    int newItemID = SequenceGenerator.GetNextVal("KP_Item_ID");
                    new_item.Item_ID = SequenceGenerator.GetNextVal("KP_Item_ID"); ;
                    new_item.Item_Name = kpitem.Item_Name;
                    new_item.Min_Score = kpitem.Min_Score;
                    new_item.Max_Score = kpitem.Max_Score;
                    new_item.KP_ID = newID;
                    new_item.Item_Quotiety = kpitem.Item_Quotiety;
                    new_item.Is_Group = kpitem.Is_Group;
                    new_item.Parent_Item_ID = newItemID ;
                    new_item.Item_Type = kpitem.Item_Type;
                    new_item.Item_Width = kpitem.Item_Width;
                    new_item.Item_Default = kpitem.Item_Default;
                    new_item.Item_Desc = kpitem.Item_Desc;

                    kpitem.MoveNext();
                }


                kp.MoveNext();
            }

        }

        private void CopyKp(int oldID ,KP_Master new_kp)
        {
            KP_Master kp = new KP_Master();
            kp.LoadByPrimaryKey(oldID);
            new_kp.AddNew();
            new_kp.KP_ID = SequenceGenerator.GetNextVal("KP_ID");
            //项目名称 + 表名
            //new_kp.Title = dplSubject.SelectedValue + txtName.Text;
            new_kp.Title = kp.Title;
            new_kp.Dept_ID = kp.Dept_ID;
            new_kp.KP_Category_ID = kp.KP_Category_ID;
            new_kp.Begin_Date = kp.Begin_Date;
            new_kp.End_Date = kp.End_Date;
            new_kp.Content = kp.Content;
            new_kp.Remark = kp.Remark;
            new_kp.KpScore = kp.KpScore;
            new_kp.Status = kp.Status;
            
        }

        private void CopyRelation(int oldID, int newID , KP_Relation new_relation)
        {
            KP_Relation kp = new KP_Relation();
            kp.Where.KP_ID.Value = oldID;
            kp.Query.Load();
            while (!kp.EOF)
            {
                new_relation.AddNew();
                new_relation.Relation_ID = SequenceGenerator.GetNextVal("Relation_ID");
                new_relation.KP_ID = newID;
                new_relation.Relation_Code = kp.Relation_Code;
                new_relation.Relation_Desc = kp.Relation_Desc;
                new_relation.Relation_Quotiety = kp.Relation_Quotiety;

                kp.MoveNext();
            }

        }
    }
}
